package com.ciwong.epaper.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.util.DisplayUtils;

/**
 * Created by Administrator on 2017/12/18.
 */

public class GradeSelectDialog extends Dialog implements View.OnClickListener {

    private View mRootView;

    private OnGradeSelectListener mOnGradeSelectListener;
    private TextView selectGrade;


    public GradeSelectDialog(@NonNull Context context, OnGradeSelectListener listener) {
        super(context, R.style.ActionSheetDialogStyle);
//        mRootView= LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_grade_select, null);
        setContentView(R.layout.dialog_grade_select);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtils.getScreenWidth(context.getApplicationContext()); //设置宽度
        getWindow().setAttributes(lp);


        findViewById(R.id.btn_grade_all).setOnClickListener(this);
        findViewById(R.id.btn_grade1).setOnClickListener(this);
        findViewById(R.id.btn_grade2).setOnClickListener(this);
        findViewById(R.id.btn_grade3).setOnClickListener(this);
        findViewById(R.id.btn_grade4).setOnClickListener(this);
        findViewById(R.id.btn_grade5).setOnClickListener(this);
        findViewById(R.id.btn_grade6).setOnClickListener(this);
        findViewById(R.id.btn_grade7).setOnClickListener(this);
        findViewById(R.id.btn_grade8).setOnClickListener(this);
        findViewById(R.id.btn_grade9).setOnClickListener(this);
        findViewById(R.id.btn_grade10).setOnClickListener(this);
        findViewById(R.id.btn_grade11).setOnClickListener(this);
        findViewById(R.id.btn_grade12).setOnClickListener(this);

        findViewById(R.id.btn_grade_confirm).setOnClickListener(this);


        mOnGradeSelectListener = listener;
//        fin
    }


    public interface OnGradeSelectListener {
        void onGradeSelect(int gradeId, String gradeName);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_grade_confirm) {
            if (mOnGradeSelectListener != null && selectGrade != null) {
                int gradeId = 0;
                if (selectGrade.getId() == R.id.btn_grade1) {
                    gradeId = 1101;
                } else if (selectGrade.getId() == R.id.btn_grade2) {
                    gradeId = 1102;
                } else if (selectGrade.getId() == R.id.btn_grade3) {
                    gradeId = 1103;
                } else if (selectGrade.getId() == R.id.btn_grade4) {
                    gradeId = 1104;
                } else if (selectGrade.getId() == R.id.btn_grade5) {
                    gradeId = 1105;
                } else if (selectGrade.getId() == R.id.btn_grade6) {
                    gradeId = 1106;
                } else if (selectGrade.getId() == R.id.btn_grade7) {
                    gradeId = 1107;
                } else if (selectGrade.getId() == R.id.btn_grade8) {
                    gradeId = 1108;
                } else if (selectGrade.getId() == R.id.btn_grade9) {
                    gradeId = 1109;
                } else if (selectGrade.getId() == R.id.btn_grade10) {
                    gradeId = 1111;
                } else if (selectGrade.getId() == R.id.btn_grade11) {
                    gradeId = 1112;
                } else if (selectGrade.getId() == R.id.btn_grade12) {
                    gradeId = 1113;
                } else if (selectGrade.getId() == R.id.btn_grade_all) {
                    gradeId = 0;
                }
                mOnGradeSelectListener.onGradeSelect(gradeId, selectGrade.getText().toString());
            }
            dismiss();

        } else {

            if (null != selectGrade) {
                selectGrade.setBackgroundResource(R.drawable.btn_bg_gray_border4);
                selectGrade.setTextColor(getContext().getResources().getColor(R.color.light_gray16));
            }
            if (v instanceof TextView) {
                selectGrade = (TextView) v;
                selectGrade.setBackgroundResource(R.drawable.selector_btn_bg_light_green_radius15dp);
                selectGrade.setTextColor(getContext().getResources().getColor(R.color.white));
            }

        }

    }


    public static String getGradeNameById(int gradeId) {
        String gradeName = "range_all";

        if (gradeId == 1101) {
            gradeName = "一年级";
        } else if (gradeId == 1102) {
            gradeName = "二年级";
        } else if (gradeId == 1103) {
            gradeName = "三年级";
        } else if (gradeId == 1104) {
            gradeName = "四年级";
        } else if (gradeId == 1105) {
            gradeName = "五年级";
        } else if (gradeId == 1106) {
            gradeName = "六年级";
        } else if (gradeId == 1107) {
            gradeName = "七年级";
        } else if (gradeId == 1108) {
            gradeName = "八年级";
        } else if (gradeId == 1109) {
            gradeName = "九年级";
        } else if (gradeId == 1111) {
            gradeName = "高一";
        } else if (gradeId == 1112) {
            gradeName = "高二";
        } else if (gradeId == 1113) {
            gradeName = "高三";
        } else {
        }

        return gradeName;
    }
}
